package com.kern.academic.controller;

import com.kern.academic.bean.ResponseResult;
import com.kern.academic.cache.CacheManager;
import com.kern.academic.cache.constant.RedisCacheKey;
import com.kern.academic.dto.MenuDTO;
import com.kern.academic.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * Created by caoshi at 8:13 2021-07-01
 */
@RestController
public class MenuController {

    @Autowired
    private IMenuService menuService;

    @Autowired
    private CacheManager cacheManager;


    @RequestMapping("/menus")
    public ResponseResult listAllMenus() throws IOException, ClassNotFoundException {
        @SuppressWarnings("unchecked")
        List<MenuDTO> mlist = (List<MenuDTO>) cacheManager.getObject(RedisCacheKey.LIST_ALLMENUS_KEY);
        if (mlist != null) {
            return ResponseResult.ok("查询菜单操作成功!", mlist);
        } else {
            List<MenuDTO> menuList = menuService.listAll();
            if (menuList.size() > 0) {
                cacheManager.cache(RedisCacheKey.LIST_ALLMENUS_KEY, menuList);
                return ResponseResult.ok("查询菜单操作成功!", menuList);
            } else {
                return ResponseResult.fail("查询菜单失败!");
            }
        }
    }

}
